package com.hnfnu.utils;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

/**
 * IP 工具
 */
public class IPUtils {

    /**
     * 获取用户的真实IP
     */
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public static String getRandomAddress() {
        // 定义省份列表
        String[] provinces = {
                "北京市", "天津市", "河北省", "山西省", "内蒙古自治区",
                "辽宁省", "吉林省", "黑龙江省", "上海市", "江苏省",
                "浙江省", "安徽省", "福建省", "江西省", "山东省",
                "河南省", "湖北省", "湖南省", "广东省", "广西壮族自治区",
                "海南省", "重庆市", "四川省", "贵州省", "云南省",
                "西藏自治区", "陕西省", "甘肃省", "青海省", "宁夏回族自治区",
                "新疆维吾尔自治区", "台湾省", "香港特别行政区", "澳门特别行政区"
        };

        // 创建随机数生成器
        Random random = new Random();

        // 生成随机索引
        int randomIndex = random.nextInt(provinces.length);

        // 获取随机选择的省份
        String randomProvince = provinces[randomIndex];
        return randomProvince;
    }
}
